package com.zzw.controller;

import com.zzw.bean.Store;
import com.zzw.bean.Msg;
import com.zzw.service.StoreService;
import com.zzw.utils.OssUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 门店管理接口
 *
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Controller
@RequestMapping("/store")
public class StoreController {

    @Resource
    private StoreService storeService;

    @GetMapping("/selectStoreList")
    @ResponseBody
    public Msg selectStoreList() {
        List<Store> storeList = storeService.selectStoreList();
        return Msg.success().add("storeList", storeList);
    }

    @GetMapping("/selectStoreById")
    @ResponseBody
    public Msg selectStoreById(Integer id) {
        Store store = storeService.selectStoreById(id);
        return Msg.success().add("store", store);
    }

    @PostMapping("/insertStore")
    @ResponseBody
    //解读：https://www.doubao.com/share/doc/412680c1d2d6a494
    //https://www.doubao.com/thread/w1ef88bb1e5438a59
    public Msg insertStore(@RequestPart("picture") MultipartFile picture,
                           @RequestPart("store") Store store) throws IOException {
        if (picture == null || picture.isEmpty() || !picture.getContentType().startsWith("image")) {
            return Msg.fail().add("msg", "请上传图片");
        }
        String objectName = UUID.randomUUID().toString() + picture.getOriginalFilename();
        String picUrl = OssUtil.uploadStream(picture.getInputStream(), objectName, picture.getContentType());
        store.setPicture(picUrl);
        int result = storeService.insertStore(store);
        return Msg.success().add("store", store);
    }

    @PutMapping("/updateStoreStatus")
    @ResponseBody
    public Msg updateStoreStatus(Integer id) {
        int result = storeService.updateStoreStatus(id);
        return Msg.success().add("result", result);
    }
}
